using System;
using System.Collections.Generic;
using System.Linq;
using GP.Logic.Model;

namespace GP.Logic
{
    public class CitizenService
    {
        private readonly GpContext _context = new GpContext();

        public ICollection<Citizen> GetCitizens()
        {
            return _context.Citizens.ToList();
        }

        public ICollection<Event> GetEvents(long citizenId)
        {
            return _context.Events.Where(a => a.CitizenId == citizenId).ToList();
        }

        public Event CreateEvent(long citizenId, string eventType, string subject, string maintext, string equipment)
        {
            var e = new Event
                          {
                              CitizenId = citizenId,
                              EventType = eventType,
                              TimeCreated = DateTime.Now,
                              Subject = subject,
                              MainText = maintext,
                              Equipment = equipment
                          };
            _context.Events.Add(e);
            _context.SaveChanges();
            return e;
        }

        public Citizen GetCitizen(long citizenId)
        {
            return _context.Citizens.Single(a => a.CitizenId == citizenId);
        }

        public void DeleteCitizen(long citizenId)
        {
            Citizen citizen = GetCitizen(citizenId);
            _context.Citizens.Remove(citizen);
            _context.SaveChanges();
        }

        public Citizen CreateCitizen()
        {
            long docId = _context.Doctors.First().DocId;
            var item = new Citizen
                           {
                               Email = "",
                               Phone = "",
                               PassKey = Guid.NewGuid().ToString("N").Substring(0, 12),
                               Name = "name",
                               TimeCreated = DateTime.Now,
                               DocId = docId,
                               SocialSecurity = Guid.NewGuid().ToString("N").Substring(0, 12),
                           };

            _context.Citizens.Add(item);
            _context.SaveChanges();

            return item;
        }

        public void SaveCitizen(long citizenId, Citizen citizen)
        {
            Citizen old = GetCitizen(citizenId);
            old.Email = citizen.Email ?? "";
            old.Phone = citizen.Phone ?? "";
            old.PassKey = citizen.PassKey ?? "";
            old.Name = citizen.Name ?? "";
            old.SocialSecurity = citizen.SocialSecurity ?? "";
            old.DocId = citizen.DocId;
            
            _context.SaveChanges();
        }

        public void DeleteAccess(long citizenId, long docId)
        {
            var to = _context.GrantedAccessTos.Single(a => a.DocId == docId && a.CitizenId == citizenId);
            _context.GrantedAccessTos.Remove(to);
            _context.SaveChanges();
        }

        public void CreateAccess(long citizenId, long docId, DateTime expires)
        {
            var item = new GrantedAccessTo();
            item.DocId = docId;
            item.CitizenId = citizenId;
            item.Expires = expires;
            _context.GrantedAccessTos.Add(item);
            _context.SaveChanges();
        }
    }
}